package com.shujia.flink.core

import java.util.Properties

import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer

object Demo8SavePoint {
  def main(args: Array[String]): Unit = {


    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    val properties = new Properties()
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")
    properties.setProperty("group.id", "asdasssssdasdasdadadas")
    properties.setProperty("auto.offset.reset", "earliest")

    val consumer = new FlinkKafkaConsumer[String]("checkpoint", new SimpleStringSchema(), properties)


    /**
      * 读取kafka数据的source task 是有状态的，（保存第是消费偏移量）
      *
      */

    val kafkaDS: DataStream[String] = env.addSource(consumer).uid("source")


    /**
      * 只有有状态算子需要设置uid
      * uid 由于识别算子，保证后面代码修改后还可以从checkpoint中恢复
      *
      */


    kafkaDS
      .flatMap(_.split(","))
      .filter(word => word != "null")
      .map((_, 1))
      .keyBy(_._1)
      .sum(1).uid("sum")
      .print()


    env.execute()

    /**
      * 触发savepoint
      * flink savepoint aed047e9e3dc52c6e4f3971b62af4baa hdfs://master:9000/flink/savepoint -yid application_1622427588775_0007
      *
      */


  }

}
